package com.csweimei.rentmanagementtreasure;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.databinding.ActivityWebBinding;
import com.csweimei.rentmanagementtreasure.viewModel.WebVM;

import me.goldze.mvvmhabit.base.BaseActivity;

public class WebActivity extends BaseActivity<ActivityWebBinding, WebVM> {

    String flag;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_web;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
        flag = getIntent().getStringExtra("flag");

    }

    @Override
    public void initData() {
        super.initData();
        initWeb();
    }

    private void initWeb() {
        WebSettings webSettings = binding.web.getSettings();
        // 运行js访问
        webSettings.setJavaScriptEnabled(true);//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        binding.web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            // 链接跳转都会走这个方法
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);// 强制在当前 WebView 中加载 url
                return true;
            }
        });

        binding.web.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });

        if (flag.equals("YSZC")) {
            viewModel.setTitleText("隐私政策");
            binding.web.loadUrl(AppConfig.YSZC_URL + getResources().getString(R.string.app_name));
        } else if (flag.equals("VIP")) {
            viewModel.setTitleText("会员用户协议");
            binding.web.loadUrl(AppConfig.VIP_URL + getResources().getString(R.string.app_name));
        } else {
            viewModel.setTitleText("用户协议");
            binding.web.loadUrl(AppConfig.YHXY_URL + getResources().getString(R.string.app_name));
        }

    }
}